package cn.javaxc.mvcwork.framework.filters;
import java.io.IOException;
import javax.servlet.*;






public final class ExampleFilter implements Filter {


    // ----------------------------------------------------- Instance Variables


    
    private String attribute = null;


    
    private FilterConfig filterConfig = null;


    // --------------------------------------------------------- Public Methods


    
    public void destroy() {

        this.attribute = null;
        this.filterConfig = null;

    }


    
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
	throws IOException, ServletException {

	// Store ourselves as a request attribute (if requested)
	if (attribute != null)
	    request.setAttribute(attribute, this);

	// Time and log the subsequent processing
	long startTime = System.currentTimeMillis();
        chain.doFilter(request, response);
	long stopTime = System.currentTimeMillis();
	filterConfig.getServletContext().log
	    (this.toString() + ": " + (stopTime - startTime) +
	     " milliseconds");

    }


    
    public void init(FilterConfig filterConfig) throws ServletException {

	this.filterConfig = filterConfig;
        this.attribute = filterConfig.getInitParameter("attribute");

    }


    
    public String toString() {

	if (filterConfig == null)
	    return ("InvokerFilter()");
	StringBuffer sb = new StringBuffer("InvokerFilter(");
	sb.append(filterConfig);
	sb.append(")");
	return (sb.toString());

    }


}
